<?php

namespace Es\Operate;

use Es\Client;
use Es\Exceptions\CommonExceptions;
use Es\Struct\{ConnectStruct,DocumentBulkStruct};

class DocumentBulk extends Client
{
    /**
     * @var DocumentBulkStruct
     */
    private $document;

    public function __construct(ConnectStruct $connect, DocumentBulkStruct $document)
    {
        parent::__construct($connect);

        $this->document = $document;
    }

    /**
     * @return array
     * @throws CommonExceptions
     */
    public function add(): array
    {
        if(empty($this->document->body)){
            throw new  CommonExceptions(CommonExceptions::DOCU_BODY_NOT_EMPTY);
        }
        $sliceData = array_chunk($this->document->body, $this->document->bulkNum);
        try {
            if(empty($this->document->blukidFileds)){
                $response = $this->autoId($sliceData);
            }else{
                $response = $this->noAutoId($sliceData, $this->document->blukidFileds);
            }
        } catch (\Throwable $e) {
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;
    }


    /**
     * @return array
     * @throws CommonExceptions
     */
    public function delete(): array
    {
        if(empty($this->document->ids)){
            throw new  CommonExceptions(CommonExceptions::DOCU_ID_NOT_EMPTY);
        }
        $sliceIds = array_chunk($this->document->ids, 1000);
        try {
            $response = [];
            foreach ($sliceIds as $chunk) {
                $params = [];
                foreach ($chunk as $value) {
                    $params['body'][] = [
                        'delete' => [
                            '_index' => $this->document->index,
                            '_type'  => $this->document->type,
                            '_id'    => $value
                        ],
                    ];
                }
                $response[] = self::$client->bulk($params);
            }
        } catch (\Throwable $e) {
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;
    }

    /**
     * @param $sliceData
     * @return array
     */
    private function autoId($sliceData): array
    {
        $response = [];
        foreach ($sliceData as $chunk) {
            $params = [];
            foreach ($chunk as $value) {
                $params['body'][] = [
                    'index' => [
                        '_index' => $this->document->index,
                        '_type'  => $this->document->type,
                    ],
                ];
                $params['body'][] = $value;
            }
            $response[] = self::$client->bulk($params);
        }
        return $response;
    }

    /**
     * @param $sliceData
     * @param $bulkIdFields
     * @return array
     */
    private function noAutoId($sliceData, $bulkIdFields): array
    {
        $response = [];
        foreach ($sliceData as $chunk) {
            $params = [];
            foreach ($chunk as $value) {
                $params['body'][] = [
                    'index' => [
                        '_index' => $this->document->index,
                        '_type'  => $this->document->type,
                        '_id'    => $value[$bulkIdFields]
                    ],
                ];
                unset($value[$bulkIdFields]);
                $params['body'][] = $value;
            }
            $response[] = self::$client->bulk($params);
        }
        return $response;
    }
}
